"""
=================================================
@Author: Zenon
@Date: 2025-05-21
@Description：异常检测界面模块
==================================================
"""
from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
                              QTextEdit, QComboBox, QGroupBox, QLineEdit)
from PySide6.QtGui import QFont

class DetectionPage(QWidget):
    """异常检测界面类"""
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.font = parent.font if parent else QFont("Arial", 12)
        self.param_line_edits = []
        self.init_ui()
        
    def init_ui(self):
        """初始化UI"""
        layout = QHBoxLayout(self)
        layout.setContentsMargins(15, 15, 15, 15)
        layout.setSpacing(20)

        # 左侧参数设置区域
        left_widget = QWidget()
        left_widget.setMinimumWidth(300)
        left_layout = QVBoxLayout(left_widget)
        left_layout.setSpacing(15)

        # 模型选择区域
        model_group = QGroupBox("模型选择")
        model_group.setFont(self.font)
        model_layout = QVBoxLayout(model_group)

        model_label = QLabel("选择检测模型:")
        model_label.setFont(self.font)
        self.model_combobox = QComboBox()
        self.model_combobox.setMinimumHeight(35)
        self.model_combobox.setFont(self.font)

        model_layout.addWidget(model_label)
        model_layout.addWidget(self.model_combobox)

        # 参数设置区域
        self.param_group = QGroupBox("参数设置")
        self.param_group.setFont(self.font)
        self.param_layout = QVBoxLayout(self.param_group)
        self.param_layout.setSpacing(10)

        # 结果显示区域
        result_group = QGroupBox("检测结果")
        result_group.setFont(self.font)
        result_layout = QVBoxLayout(result_group)

        self.detection_result_textedit = QTextEdit()
        self.detection_result_textedit.setPlaceholderText("检测结果将显示在这里...")
        self.detection_result_textedit.setReadOnly(True)
        self.detection_result_textedit.setFont(self.font)
        self.detection_result_textedit.setMinimumHeight(200)

        result_layout.addWidget(self.detection_result_textedit)

        # 按钮区域
        btn_layout = QHBoxLayout()
        self.detect_button = QPushButton("执行检测")
        self.detect_button.setMinimumHeight(40)
        self.detect_button.setStyleSheet("""
            QPushButton {
                background-color: #007acc;
                color: white;
                border-radius: 5px;
                font-size: 14px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #005fa3;
            }
            QPushButton:pressed {
                background-color: #004c80;
            }
        """)
        self.detect_button.setFont(QFont("Arial", 14, QFont.Bold))

        btn_layout.addStretch()
        btn_layout.addWidget(self.detect_button)
        btn_layout.addStretch()

        left_layout.addWidget(model_group)
        left_layout.addWidget(self.param_group)
        left_layout.addStretch()
        left_layout.addLayout(btn_layout)

        layout.addWidget(left_widget, 1)
        
    def update_param_inputs(self, model_name, params):
        """根据选择的模型更新参数输入框"""
        # 清空现有参数输入框
        while self.param_layout.count():
            item = self.param_layout.takeAt(0)
            widget = item.widget()
            if widget:
                widget.deleteLater()

        # 创建新的参数输入框
        self.param_line_edits = []
        for param_name in params:
            label_widget = QLabel(param_name)
            label_widget.setStyleSheet("color: #007acc; font-weight: bold;")
            label_widget.setFont(self.font)
            self.param_layout.addWidget(label_widget)

            edit = QLineEdit()
            edit.setMinimumHeight(30)
            edit.setFont(self.font)
            self.param_layout.addWidget(edit)
            self.param_line_edits.append(edit)